<?php

/**
 * Ajax_EditEvent.phtml - Edition d'un évènement
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */

/**
 * decode_post_data() - Décode un tableau $_POST
 * 
 * @param type $input
 * @return type
 */
function decode_post_data($input) {
    $resultArray = array();
    if (is_array($input)) {
        foreach ($input as $key => $value) {
            $resultArray[$key] = is_string($value) ? utf8_encode(base64_decode($value)) : $value;
        }
    }
    return $resultArray;
}

$post = decode_post_data($_POST);

if (isset($post['repeat_on_specific_days']) && is_array($post['repeat_on_specific_days'])) {
    $jours = array(0 => 'monday', 1 => 'tuesday', 2 => 'wednesday', 3 => 'thursday', 4 => 'friday', 5 => 'saturday', 6 => 'sunday');
    foreach ($post['repeat_on_specific_days'] as $key => $item_id) {
        $intKey = (int) str_replace('repeat_on_specific_days_', '', $item_id);
        $post['repeat_on_specific_days'][$key] = $jours[$intKey];
    }
    $post['repeat_on_specific_days'] = implode(';', $post['repeat_on_specific_days']);
} else {
    $post['repeat_on_specific_days'] = '';
}
// We have integrity here, save to database

$login_infos = Erra_Persistance_Session::get('login_infos');
if (is_object($login_infos)) {
    // if exists, update, else create id, event_id, agenda_id
    Erra_Core_DatabasePdo::SELECT('SELECT id FROM event_list WHERE event_id="' . $post['id'] . '")');
    if (Erra_Core_DatabasePdo::rowCount()) {
        // Event exists
        // Event doesn't exists
        $query = 'UPDATE event_list SET 
            agenda_id="' . $post['agenda_id'] . '",
            slot_start="' . $post['slot_start'] . '", 
            slot_end="' . $post['slot_end'] . '", 
            slot_day="' . $post['slot_day'] . '", 
            title="' . $post['title'] . '", 
            body="' . $post['body'] . '", 
            event_color="' . $post['event_color'] . '", 
            repeat_enabled="' . (int) ((boolean) $post['repeat_enabled']) . '", 
            repeat_time="' . $post['repeat_time'] . '", 
            repeat_period="' . $post['repeat_period'] . '", 
            repeat_occurency="' . $post['repeat_occurency'] . '", 
            repeat_occurency_date="' . $post['repeat_occurency_date'] . '", 
            repeat_on_specific_days="' . $post['repeat_on_specific_days'] . '",
            WHERE event_id="' . $post['id'] . '"';

        Erra_Core_DatabasePdo::EXECUTE($query);
    } else {
        // Event doesn't exists
        $query = 'INSERT INTO event_list(event_id, agenda_id, slot_start, slot_end, slot_day, title, body, event_color, 
            repeat_enabled, repeat_time, repeat_period, repeat_occurency, repeat_occurency_date, repeat_on_specific_days)
            VALUES("' . $post['id'] . '", "' . $post['agenda_id'] . '", "' . $post['slot_start'] . '", "' . $post['slot_end'] . '", "' . $post['slot_day'] . '", "' . $post['title'] . '", "' . $post['body'] . '", "' . $post['event_color'] . '",
                "' . (int) ((boolean) $post['repeat_enabled']) . '", "' . $post['repeat_time'] . '", "' . $post['repeat_period'] . '", "' . $post['repeat_occurency'] . '", "' . $post['repeat_occurency_date'] . '", "' . $post['repeat_on_specific_days'] . '")';
        Erra_Core_DatabasePdo::EXECUTE($query);
    }
}